/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.flan.stock.activity;

import java.util.List;

import com.flan.stock.R;
import com.flan.stock.adapter.StockRankDataAdapter;
import com.flan.stock.bean.Stock;
import com.flan.stock.service.StockService;
import com.flan.stock.service.UIListener;
import com.flan.stock.utils.Constant;
import com.flan.stock.utils.DensityUtil;
import com.flan.stock.view.FooterMenuView;
import com.flan.stock.view.HVListView;
import com.flan.stock.view.SimpleCommonTitleView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;

/**
 * 显示沪深A股大盘实时数据的窗口界面
 * @author flan
 * @since  2016年4月18日
 */
public class HSAStockActivity extends Activity implements OnClickListener,UIListener{
	
	//通用窗口title
	private SimpleCommonTitleView simpleTitle;
	private FooterMenuView menu;
	private HVListView hvlistView;
	private StockRankDataAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_hsastock);
		
		//请求远程服务器上的股票排序数据
		StockService.getQuotationRank(0,10,Constant.STOCK_COLUMN_NOWPRICE,Constant.ORDER_BY_DESC,1001,this);
		
		//初始化titlebar
		simpleTitle = (SimpleCommonTitleView) this.findViewById(R.id.simple_title);
		simpleTitle.setOnLeftClickListener(this);
		simpleTitle.setOnRightClickListener(this);
		
		//初始化底部菜单控件
		menu = (FooterMenuView) this.findViewById(R.id.footer_menu);
		menu.setCurrentMenu(R.id.menu_hangqing);
		menu.setOnTouchListener(menu.new MenuOnTouchListener());
		
		hvlistView = (HVListView) this.findViewById(R.id.hv_list);
		//设置可滚动的列头
		hvlistView.setListHead((LinearLayout) findViewById(R.id.scrollColumn));
		//设置可滚动空间的宽度
		hvlistView.setScrollWidth(DensityUtil.dip2px(this, 630+70+10));
		adapter = new StockRankDataAdapter(this,hvlistView);
		
		hvlistView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//Intent intent = new Intent(HSAStockActivity.this,StockDetailActivity.class);
				//startActivity(intent);
			}
		});
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		//设置数据
		hvlistView.setAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_right_but:
				toSearch();
				break;
			case R.id.title_left_but:
				HSAStockActivity.this.finish();
				break;
		}
	}
	
	/**
	 * 进入股票代码搜索页面
	 */
	private void toSearch(){
		
	}

	@Override
	public void refresh(Object... params) {
		List<Stock> stockList = (List<Stock>) params[0];
		for(Stock s : stockList){
			System.out.println(s.getName());
		}
		adapter.setData(stockList);
		adapter.notifyDataSetInvalidated();
	}
	
}
